package com.lu.manage.core.xss;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;

/**
 * @program LuBoot
 * @description:
 * @author: zhanglu
 * @create: 2019-10-14 21:36:00
 */
public class XssFilter implements Filter {

    FilterConfig filterConfig = null;

    private List<String> urlExclusion = null;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        this.filterConfig = filterConfig;
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest servletRequest = (HttpServletRequest) request;
        String servletPath = servletRequest.getServletPath();
        if(urlExclusion != null && urlExclusion.contains(servletPath)){
            chain.doFilter(request, response);
        }else {
            chain.doFilter(new XssHttpServletRequestWrapper((HttpServletRequest)request), response);
        }
    }

    @Override
    public void destroy() {
        this.filterConfig = null;
    }

    public List<String> getUrlExclusion() {
        return urlExclusion;
    }

    public void setUrlExclusion(List<String> urlExclusion) {
        this.urlExclusion = urlExclusion;
    }
}
